package com.hmall.item.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.constant.MqConstant;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 分页返回商品数据
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> getItemList(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
        Page<Item> itemPage = new Page<>(page, size);
        Page<Item> itemPageList = itemService.page(itemPage);
        return new PageDTO<>(itemPageList.getTotal(),itemPageList.getRecords() );
    }

    /**
     * 根据id和status状态来更新商品状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    public String updateItemById(@RequestBody @PathVariable Long id, @RequestBody @PathVariable Integer status){
        Item item = itemService.getById(id);
        if (!(item.getStatus().equals(status))){
            item.setStatus(status);
        }else {
           return "商品已为上架或下架状态，请刷新页面";
        }
        boolean b = itemService.updateById(item);
        if (b==false){
            return "商品上架状态更新失败";
        }
        rabbitTemplate.convertAndSend(MqConstant.ITEM_EXCHANGE,MqConstant.ITEM_INSERT_KEY, JSON.toJSONString(item));
        return "更新成功";
    }

    /**
     * 新增商品
     * @param item
     * @return
     */
    @PostMapping
    public String saveItem(@RequestBody Item item){
        LambdaQueryWrapper<Item> itemQW = new LambdaQueryWrapper<>();
        itemQW.eq(Item::getId,item.getId());
        Item one = itemService.getOne(itemQW);
        if (one!=null){
            Long id = one.getId();
            return "商品已经存在请先删除id:"+id+"的商品";
        }
        boolean save = itemService.save(item);
        if (save==false){
            return "新增数据失败";
        }
        rabbitTemplate.convertAndSend(MqConstant.ITEM_EXCHANGE,MqConstant.ITEM_INSERT_KEY,JSON.toJSONString(item));
        return "新增成功";
    }

    /**
     * 通过id删除商品
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public String deleteItemById(@PathVariable Long id){
        if (id==null){
            return "id不能为空";
        }
        Item item = itemService.getById(id);
        if (item==null){
            return "商品不存在";
        }
        boolean b = itemService.removeById(id);
        if (b==false){
            return "商品删除失败";
        }
        rabbitTemplate.convertAndSend(MqConstant.ITEM_EXCHANGE,MqConstant.ITEM_DELETE_KEY,id.toString());
        return "删除成功";
    }

    /**
     * 修改商品
     * @param item
     * @return
     */
    @PutMapping
    public String dateupItem(@RequestBody Item item){
        if (item.getId()==null){
            return "商品id不能为空";
        }
        boolean b = itemService.updateById(item);
        if (b==false){
            return "商品修改失败";
        }
        rabbitTemplate.convertAndSend(MqConstant.ITEM_EXCHANGE,MqConstant.ITEM_INSERT_KEY,JSON.toJSONString(item));
        return "商品修改成功";
    }

    /**
     * 订单页面返回
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getItemById(@PathVariable("id") String id){
        if (id==null&&"".equals(id)){
            return new Item();
        }
        Item item = itemService.getById(Long.valueOf(id));
        if (item==null) {
            log.warn("item值为空");
        }
        return item;
    }
    @GetMapping("/item/{id}/{num}")
    public boolean updateStock(@PathVariable("id") Long id,@PathVariable("num") Integer num){
        boolean b = itemService.updateStock(id, num);
        if (!b){
            return false;
        }
        return true;
    }
}
